WorkerScript.onMessage = function(message) {
    doLoadSuggestedSpots(message);
}


function doLoadSuggestedSpots(message) {
    if (message.token == message.g.defaultToken) {
        message.model.clear();
        message.model.sync();
        WorkerScript.sendMessage({ 'result': 'SUCCESS', 'listState': '' });
        return;
    }

    var xhr = new XMLHttpRequest;
    var url = "https://production-2.sogeoapi.com/api/poi/suggested?per_page=" + message.g.perPage + "&order=random&oauth_token=" + message.token;

    xhr.open("GET", url);
    xhr.onreadystatechange = function() {
        try {
            if (xhr.readyState == XMLHttpRequest.DONE) {
                message.model.clear();
                var response = JSON.parse(xhr.responseText);
                for (var i in response.data) {
                    var spot = response.data[i].poi;
                    message.model.append({
                                  spotName: spot.name,
                                  index: i,
                                  spotTags: spot.tag_list != null ? spot.tag_list : ""
                    });
                }
                message.model.sync();
                WorkerScript.sendMessage({ 'result': 'SUCCESS', 'listState': '' })
            }
        } catch(err) {
            WorkerScript.sendMessage({ 'result': 'FAILURE', 'message': err + ", " + xhr.responseText, 'listState': '' });
        }

    }
    xhr.send();
}
